﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccess
{
    public partial class AuditLogDao
    {
        public int SearchCount(string operationCode, string source, string category, string userName, DateTime insertTimestamp)
        {
            int count = 0;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_AuditLog_Search_Count";
            command.Parameters.Add(new SqlParameter("@OperationCode", operationCode));
            command.Parameters.Add(new SqlParameter("@Source", source));
            command.Parameters.Add(new SqlParameter("@Category", category));
            command.Parameters.Add(new SqlParameter("@UserName", userName));
            command.Parameters.Add(new SqlParameter("@InsertTimestamp", insertTimestamp));

            try
            {
                connection.Open();

                count = (int)command.ExecuteScalar();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return count;
        }

        public Collection<AuditLog> Search(string operationCode, string source, string category, string userName, DateTime insertTimestamp, int pageIndex, int pageSize)
        {
            Collection<AuditLog> auditLogs = new Collection<AuditLog>();

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_AuditLog_Search";
            command.Parameters.Add(new SqlParameter("@OperationCode", operationCode));
            command.Parameters.Add(new SqlParameter("@Source", source));
            command.Parameters.Add(new SqlParameter("@Category", category));
            command.Parameters.Add(new SqlParameter("@UserName", userName));
            command.Parameters.Add(new SqlParameter("@InsertTimestamp", insertTimestamp));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        AuditLog auditLog = CreateEntityByReader(reader);
                        auditLogs.Add(auditLog);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return auditLogs;
        }

        public void DeleteAll()
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_AuditLog_DeleteAll";

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }
    }
}
